package com.example.flower_shop.controller;

import com.example.flower_shop.common.ApiResponse;
import com.example.flower_shop.dto.category.CategoryVO;
import com.example.flower_shop.entity.Category;
import com.example.flower_shop.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Validated
@RestController
@RequestMapping("/api/categories")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @PostMapping("/init")
    public ApiResponse<List<CategoryVO>> initCategories() {
        // 删除所有现有分类
        List<Category> existingCategories = categoryService.findAll();
        for (Category category : existingCategories) {
            categoryService.deleteById(category.getId());
        }

        // 创建新分类
        List<Category> categories = new ArrayList<>();
        categories.add(categoryService.createCategory("鲜花", "各类新鲜花束、花篮等"));
        categories.add(categoryService.createCategory("绿植", "室内外观赏植物、盆栽等"));

        // 返回所有分类
        List<CategoryVO> categoryVOs = categories.stream()
                .map(CategoryVO::new)
                .collect(Collectors.toList());
        return ApiResponse.success("分类初始化成功", categoryVOs);
    }

    @GetMapping
    public ApiResponse<List<CategoryVO>> listCategories() {
        List<CategoryVO> categories = categoryService.findAll().stream()
                .map(CategoryVO::new)
                .collect(Collectors.toList());
        return ApiResponse.success(categories);
    }

    @GetMapping("/search")
    public ApiResponse<List<CategoryVO>> searchCategories(@RequestParam String keyword) {
        List<CategoryVO> categories = categoryService.findByNameContaining(keyword).stream()
                .map(CategoryVO::new)
                .collect(Collectors.toList());
        return ApiResponse.success(categories);
    }

    @PostMapping
    public ApiResponse<CategoryVO> createCategory(
            @RequestParam @NotBlank(message = "分类名称不能为空") String name,
            @RequestParam(required = false) String description) {
        Category category = categoryService.createCategory(name, description);
        return ApiResponse.success(new CategoryVO(category));
    }

    @PutMapping("/{id}")
    public ApiResponse<CategoryVO> updateCategory(
            @PathVariable Long id,
            @RequestParam @NotBlank(message = "分类名称不能为空") String name,
            @RequestParam(required = false) String description) {
        Category category = categoryService.updateCategory(id, name, description);
        return ApiResponse.success(new CategoryVO(category));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteById(id);
        return ApiResponse.success(null);
    }
}